﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace Thor.Repository.Azure.Table
{
    /// <summary>
    /// Represents the InvalidLogons table.
    /// </summary>
    internal class InvalidLogonContext : TableServiceContext
    {
               #region Fields

        private static object _lockObject = new Object();

        #endregion

        #region Constants

        private const string TABLENAME = "InvalidLogns";

        #endregion

        #region Constructors

        public InvalidLogonContext(string uri, StorageCredentials storageCredentials)
            : base(uri, storageCredentials)
        {

        }

        #endregion

        #region Methods

        /// <summary>
        /// Inserts a new row into the table.
        /// </summary>
        /// <param name="row"></param>
        public void Insert(InvalidLogonRow row)
        {
            CreateTable();
            this.AddObject(TABLENAME, row);
            this.SaveChanges();
        }

        /// <summary>
        /// Updates a row in the table.
        /// </summary>
        /// <param name="row"></param>
        public void Update(InvalidLogonRow row)
        {
            if (DoesTableExist())
            {
                InvalidLogonRow currentRow = this.GetRow(row.Id);

                if (currentRow != null)
                {
                    Delete(row);
                    Insert(row);
                }
            }
        }

        /// <summary>
        /// Deletes a row from the table.
        /// </summary>
        /// <param name="row"></param>
        public void Delete(InvalidLogonRow row)
        {
            if (DoesTableExist())
            {
                InvalidLogonRow currentRow = this.GetRow(row.Id);

                if (currentRow != null)
                {
                    this.DeleteObject(currentRow);
                    this.SaveChanges();
                }
            }
        }

        /// <summary>
        /// Creates the table if it does not exist.
        /// </summary>
        private void CreateTable()
        {
            lock (_lockObject)
            {
                if (!DoesTableExist())
                {
                    CloudTableClient tableClient = new CloudTableClient(this.BaseUri.AbsoluteUri, this.StorageCredentials);
                    tableClient.CreateTable(TABLENAME);
                }
            }
        }

        /// <summary>
        /// Determines if the table exists.
        /// </summary>
        /// <returns></returns>
        private bool DoesTableExist()
        {
            CloudTableClient tableClient = new CloudTableClient(this.BaseUri.AbsoluteUri, this.StorageCredentials);
            return tableClient.DoesTableExist(TABLENAME);
        }

        /// <summary>
        /// Gets a single row based on email address.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public InvalidLogonRow GetRow(Guid id)
        {
            if (DoesTableExist())
            {
                var q = this.CreateQuery<InvalidLogonRow>(TABLENAME).Where(r => r.Id == id).ToList();
                if (q.Count > 0)
                    return q[0];
                else
                    return null;
            }
            else
                return null;
        }

        /// <summary>
        /// Gets all rows.
        /// </summary>
        /// <returns></returns>
        public List<InvalidLogonRow> GetRows()
        {
            if (DoesTableExist())
                return this.CreateQuery<InvalidLogonRow>(TABLENAME).ToList();
            else
                return null;
        }

        #endregion
    }
}
